Proof of concept for function interpolation. - #48
Conversation
This makes it easier to add other kinds of interpolation later on.
Everything inside $[...] blocks is treated as a function. Right now,
only print() is supported, which just concatenates and all its arguments to a string.
So for example,
key: $[print(a, b, c)]
would resolve to
key: "a b c"
|
This looks very interesting. The only thing I'd say is that the functions should be provided (registered) as proper Python objects, rather than string-compared within a function. What do you think about that? |
|
Good idea, I will look into it. |
|
Ok, I think this is much more readable and can be extended easily if additional functions are needed. |
This is to allow python dict access in the function, like node['ip'].
This is needed because functions in a node definition might access other nodes.
This is a bit hacky. Parameter expansion inside function interpolation does not work, so $<aggregate(node[name] == ${some:value}, node[name])> fails.
|
So, this is a first working version. I resorted to always doing a complete inventory, even if only simple nodeinfo is requested. At the end of an inventory run, the functions are expanded, and each function gets the complete inventory as a parameter so it can query other hosts. Things that are missing:
|
Instead of returning a list of all extracted values, it now returns a dict mapping the nodename to the extracted values.
This did not work beforehand because functions and references were parsed at the same time, but evaluated later. This parsing failed when a reference was nested in a function. To solve this, function evaluation *and parsing* are now done after the whole inventory is available. This means that all references are already expanded, so neither the function nor the reference parsing need to be altered.
- Rename RefValue classes to something more specific:
- ReferenceStringParameter and ReferenceStringFunction represent
strings that contain references (parameters and functions
respectively)
- ReferenceParameter and ReferenceFunction represent references in
the classes above, and can be interpolates/executed
- Revert the behaviour that functions and parameters were parsed at
the same time, but functions "saved" until the whole inventory was
available. Now, functions will be parsed and executed after a
complete inventory run.
|
OK, I reworked it a bit:
The tests still pass, but I'm too lazy to add new ones for the new code. 😄 If you are still interested in the code, I would rebase and clean up the new commits and send a new pull request. |
|
Yup, #56 looks cleaner than this approach. Guess I can close this. |
Add option compose-node-name
See #47.
This is only a proof of concept and implements a simple
print()function.